package com.green.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.green.business.domain.*;
import com.green.business.mapper.SalesMapper;
import com.green.business.mapper.SalesbackMapper;
import com.green.business.service.CustomerService;
import com.green.business.service.GoodsService;
import com.green.business.service.SalesbackService;
import com.green.business.vo.SalesbackVo;
import com.green.system.common.ActiveUser;
import com.green.system.common.DataGridView;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * @Author: Green
 * @Date: 2021/11/19/17:13
 * @Description: 
 */


@Service
public class SalesbackServiceImpl extends ServiceImpl<SalesbackMapper, Salesback> implements SalesbackService{

    @Autowired
    private SalesMapper salesMapper ;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private SalesbackMapper salesbackMapper;

    @Override
    public DataGridView queryAllSalesback(SalesbackVo salesbackVo) {
        IPage<Salesback> page = new Page<>(salesbackVo.getPage(),salesbackVo.getLimit());
        QueryWrapper<Salesback> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(salesbackVo.getGoodsid()!=null,"goodsid",salesbackVo.getGoodsid());
        queryWrapper.eq(salesbackVo.getCustomerid()!=null,"customerid",salesbackVo.getCustomerid());


        queryWrapper.orderByDesc("salesbacktime");

        this.salesbackMapper.selectPage(page,queryWrapper);
        List<Salesback> records = page.getRecords();
        for (Salesback record : records) {
            if (null!=record.getGoodsid()){
                Goods good = this.goodsService.getById(record.getGoodsid());
                record.setGoodsname(good.getGoodsname());
                record.setSize(good.getSize());
            }
            if (null!=record.getCustomerid()){
                Customer customer = this.customerService.getById(record.getCustomerid());
                record.setCustomername(customer.getCustomername());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    @Override
    public Salesback saveSalesback(SalesbackVo salesbackVo) {

        //生成真实类
        Salesback salesback = new Salesback();
        salesback.setProjectid(salesbackVo.getProjectid());
        salesback.setSalesid(salesbackVo.getId());
        salesback.setGoodsid(salesbackVo.getGoodsid());
        salesback.setRemark(salesbackVo.getRemark());
        salesback.setNumber(salesbackVo.getNumber_back());
        salesback.setPaytype(salesbackVo.getPaytype());
        salesback.setCustomerid(salesbackVo.getCustomerid());
        salesback.setSalesbacktime(new Date());
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        salesback.setOperateperson(activeUser.getUser().getName());
        //保存退货信息
        this.salesbackMapper.insert(salesback);

        //修改sales表的数值
        Sales sales = this.salesMapper.selectById(salesback.getSalesid());
        sales.setNumber(sales.getNumber()-salesback.getNumber());
        this.salesMapper.updateById(sales);

        //增加库存
        Goods goods = this.goodsService.getById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()+salesback.getNumber());
        this.goodsService.updateGoods(goods);

        return salesback;
    }

    @Override
    public Integer querySalesNumberById(Integer goodsid) {
        Integer a = this.salesbackMapper.querySalesNumberById(goodsid);
        if (a == null || a.equals("")){
            return 0;
        }
        return a;
    }
}
